package com.example.demo.service.impl;

import com.example.demo.Util.Result;
import com.example.demo.Util.ResultUtil;
import com.example.demo.mapper.NewsMapper;
import com.example.demo.po.News;
import com.example.demo.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    NewsMapper newsMapper;

    @Override
    public Result SaveNews(News news) {

        if(StringUtils.isEmpty(news.getTitle())||StringUtils.isEmpty(news.getContent())){

            return ResultUtil.fail("标题、内容不能为空");

        }



        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        news.setTime(sdf.format(date));

        newsMapper.SaveNews(news);

        return ResultUtil.success(news);
    }

    @Override
    public void DeleteNews(int id) {
        newsMapper.DeleteNews(id);

    }

    @Override
    public Result UpdateNews(News news) {
        if(StringUtils.isEmpty(news.getTitle())||StringUtils.isEmpty(news.getContent())){

            return ResultUtil.fail("标题、内容不能为空");

        }

        newsMapper.UpdateNews(news);
        return ResultUtil.success("更新成功");

    }

    @Override
    public List<News> GetNews() {
        return null;
    }

    @Override
    public List<News> GetNewsByName(String name) {
        return null;
    }
}
